import type { ResponseData } from '@/utils/types.d';
import { SUCCESS_CODE } from '@/utils/types.d';
import request from '@/utils/request';
import type { DailyRevenueType, PklotRevenueTableItem, RevenueTableItem } from './types.d';

export async function queryPklotDailyRevenue(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: PklotRevenueTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    data: PklotRevenueTableItem[];
  }> = await request('/api/revenue/v1.0.0/daily', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function queryPklotHistoryRevenue(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: PklotRevenueTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    data: PklotRevenueTableItem[];
  }> = await request('/api/revenue/v1.0.0/history', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function queryHistoryRevenue(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: RevenueTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    data: RevenueTableItem[];
  }> = await request('/api/revenue/v1.0.0/total/history', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function getDailyRevenue(ownerId?: string) {
  const res: ResponseData<DailyRevenueType> = await request('/api/revenue/v1.0.0/total/daily', {
    method: 'GET',
    params: {
      ownerId,
    },
  });
  if (res?.code === SUCCESS_CODE) return { success: true, message: '请求成功', data: res.data };
  return { success: false, message: res?.message || '请求失败', data: null };
}

export async function updateHistory(_id: string) {
  const res: ResponseData<null> = await request('/api/revenue/v1.0.0/fix', {
    method: 'PUT',
    data: {
      _id,
    },
  });
  if (res?.code === SUCCESS_CODE) return { success: true, message: '更新成功', data: res.data };
  return { success: false, message: res?.message || '更新失败', data: null };
}
